<?php
require_once 'Constant.php' ;
class User_LoginController extends Zend_Controller_Action
{
    public function loginAction()
    {
        $aParams = $this->getRequest()->getParams() ;
        $filterChain = new Zend_Filter();
        $filterChain->addFilter(new Zend_Filter_Alpha())
                    ->addFilter(new Zend_Filter_StringToLower());
        
        // Filter the username
        $strLoginName = strtolower( trim( $filterChain->filter( $aParams['loginName'] ) ) ) ;
        $strPassword = trim( $aParams['password'] ) ;
        
        if( !empty( $strLoginName ) && !empty( $strPassword ) )
        {
            $objLoginTable = new Cts_Db_Login() ;
            $aLoginInfo = $objLoginTable->checkUserLogin( $strLoginName , $strPassword ) ;
            //Zend_Debug::dump( $aLoginInfo );
            if( !empty( $aLoginInfo ) )
            {
                if( $aLoginInfo['user_status'] == 1  )    
                {
                    switch( $aLoginInfo['user_type'] )
                    {
                        //admin
                        case 0 :
                            $strResult = '/cts/admin/index.html' ;
                            break ;
                        //sale
                        case 1 :
                            $strResult = '/cts/sale/index.html' ;
                            break ;
                        //engineer
                        case 2 :
                            $strResult = '/cts/engineer/index.html' ;
                            break ;
                        //custom
                        default :
                            $strResult = '/cts/custom/index.html' ;
                            break ;
                    }
                    Zend_Registry::set( 'nLoginId' , $aLoginInfo['id'] ) ;    
                    Cts_Json::echoResult( USER_VALIDED_SUCCESSFUL , $strResult ) ;
                    return ;
                }
                else
                {
                    $strResult = '/cts/login.html' ;
                    Cts_Json::echoResult( USER_VALIDED_SUCCESSFUL , $strResult ) ;    
                }
            }
            else
            {
                
            }
        }
        else
        {
            echo ( md5('123') );
        }
    }
}